//
//  HACKLineVolProvider.m
//  Pods
//
//  Created by Hotacool on 2017/9/4.
//
//

#import "HACKLineVolProvider.h"
#import "HACKLineEntry.h"
#import "HACBasicComponents.h"

@implementation HACKLineVolProvider
- (void)setData:(HACChartData *)data {
    _data = data;
    //分时数据
    _kLineData = [data dataSetWithLabel:HACKLineViewDataLabel].values;
    
    self.startIndex = MAX(0, self.actualCount - self.rootProvider.visualCount);
    self.endIndex = MAX(self.rootProvider.visualCount, self.actualCount - 1);
}

- (void)addIncrementData:(HACChartData *)data {
    NSArray *appendData = [data dataSetWithLabel:HACKLineViewDataLabel].values;
    if (appendData&&appendData.count>0) {
        _kLineData = [_kLineData arrayByAddingObjectsFromArray:appendData];
        self.startIndex = MAX(0, self.actualCount - self.rootProvider.visualCount);
        self.endIndex = MAX(self.rootProvider.visualCount, self.actualCount - 1);
        //calculate max&min
        CGFloat max, min;
        for (HACTimeLineEntry *item in appendData) {
            if ([item isKindOfClass:[HACTimeLineEntry class]]) {
                max = [self maxValueForData:item];
                min = [self minValueForData:item];
                if (max != kDrawingInvaildValue && max > self.maxValue) {
                    self.maxValue = max;
                }
                if (min != kDrawingInvaildValue && min < self.minValue) {
                    self.minValue = min;
                }
            }
        }
    }
}

- (NSInteger)actualCount {
    return _kLineData?_kLineData.count:0;
}

#pragma mark - provider protocol
- (NSArray *)rectDatasInRect:(CGRect)rect {
    if (!self.data || [self rectValueForItem:nil] == kDrawingInvaildValue) // 返回无效数据不做默认处理
        return nil;
    
    NSMutableArray *rectDrawingData = [NSMutableArray array];
    CGFloat x = 0.0, y = 0.0;
    id pointData, lastData;
    for (NSInteger i = self.startIndex; i <= self.endIndex && i < self.rootProvider.actualCount; i++) {
        pointData = self.kLineData[i];
        HACRectComponents *rectItem = [[HACRectComponents alloc] init];
        //偏移，转化到0~visualCount范围中
        NSInteger idx = [self.rootProvider relativeIdxFromIndex:i];
        x = [self.rootProvider xPointForIndex:idx inRect:self.rootProvider.pointRect];
        CGFloat value = [self rectValueForItem:pointData];
        y = [self.rootProvider calculatorYForValue:value min:self.minValue max:self.maxValue inRect:self.rootProvider.pointRect];
        rectItem.pillarRect = CGRectMake(x, y, self.rootProvider.singleWidth - self.rootProvider.itemsSpace, CGRectGetMaxY(self.rootProvider.pointRect) - y);// y坐标从上往下
        rectItem.valueType = [self calculatorValueType:pointData lastData:lastData];
        rectItem.lineColor = [self.rootProvider colorWithValueType:rectItem.valueType];
        rectItem.center = CGRectGetMidX(rectItem.pillarRect);
        [rectDrawingData addObject:rectItem];
        lastData = pointData;
    }
    return rectDrawingData;
    
}

- (NSArray *)lineDatasInRect:(CGRect)rect {
    return nil;
}

- (CGFloat)rectValueForItem:(HACKLineEntry *)data {
    return data.volume;
}

// 计算最大，最小值
- (void)calculateLimitValue {
    CGFloat max = FLT_MIN;
    CGFloat min = FLT_MAX;
    CGFloat value;
    
    for (NSInteger i = self.startIndex; i <= self.endIndex && i < self.rootProvider.actualCount; i++) {
        id model = self.kLineData[i];
        
        value = [self maxValueForData:model];
        if (value != kDrawingInvaildValue && value > max) {
            max = value;
        }
        
        value = [self minValueForData:model];
        if (value != kDrawingInvaildValue && value < min) {
            min = value;
        }
    }
    self.maxValue = (max == FLT_MIN) ? .0 : max;
    self.minValue = (min == FLT_MAX) ? .0 : min;
}

- (CGFloat)maxValueForData:(HACKLineEntry *)data {
    return data.volume;
}

- (CGFloat)minValueForData:(HACKLineEntry *)data {
    return 0;
}


- (DrawingValueType)calculatorValueType:(HACKLineEntry *)currentData lastData:(HACKLineEntry *)prePointData {
    if (currentData.closePrice > currentData.openPrice)
        return DrawingUpValue;
    else if (currentData.closePrice < currentData.openPrice)
        return DrawingDownValue;
    else // 如果收盘价＝开盘价 ，根据昨收价判断
    {
        if (currentData.closePrice > prePointData.closePrice)
            return DrawingUpValue;
        else if (currentData.closePrice < prePointData.closePrice)
            return DrawingDownValue;
        else // 如果跟昨收价还相同，保持前一天一样的逻辑
            return (DrawingValueType)prePointData.closePrice >= prePointData.openPrice;
    }
    return DrawingUpValue;
}
@end
